package com.jdzy.student.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdzy.model.teacher.pojos.AuthContextUtil;
import com.jdzy.model.teacher.pojos.SysMenu;
import com.jdzy.model.teacher.pojos.SysUser;
import com.jdzy.model.teacher.vos.SysMenuVo;
import com.jdzy.student.helper.MenuHelper;
import com.jdzy.student.mapper.SysMenuMapper;
import com.jdzy.student.mapper.SysRoleMenuMapper;
import com.jdzy.student.service.SysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.LinkedList;
import java.util.List;

@Service
@Slf4j
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    /**
     * 查询所有菜单
     * @return
     */
    @Override
    public List<SysMenu> findNodes() {
        List<SysMenu> sysMenuList = sysMenuMapper.selectAll() ;
        if (CollectionUtils.isEmpty(sysMenuList)) return null;
        List<SysMenu> treeList = MenuHelper.buildTree(sysMenuList); //构建树形数据
        return treeList;
    }

    @Override
    public List<SysMenuVo> findUserMenuList(Long id) {
        //线程获取有bug
        SysUser sysUser = AuthContextUtil.get();
//        Long userId = sysUser.getId();          // 获取当前登录用户的id
//查询数据库中的菜单
        List<SysMenu> sysMenuList = sysMenuMapper.selectListByUserId(id) ;
        //构建树形数据
        List<SysMenu> sysMenuTreeList = MenuHelper.buildTree(sysMenuList);
        return this.buildMenus(sysMenuTreeList);
    }

    // 将List<SysMenu>对象转换成List<SysMenuVo>对象
    private List<SysMenuVo> buildMenus(List<SysMenu> menus) {

        List<SysMenuVo> sysMenuVoList = new LinkedList<SysMenuVo>();
        for (SysMenu sysMenu : menus) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children = sysMenu.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }
        return sysMenuVoList;
    }

    /**
     * 保存菜单
     * @param sysMenu
     */
    @Transactional
    @Override
    public void saveMenu(SysMenu sysMenu) {
        // 添加新的节点
        sysMenuMapper.insert(sysMenu);

        // 新添加一个菜单，那么此时就需要将该菜单所对应的父级菜单设置为半开
        updateSysRoleMenuIsHalf(sysMenu);
    }

    /**
     * 修改菜单
     * @param sysMenu
     */
    @Override
    public void updateMenuById(SysMenu sysMenu) {
        sysMenuMapper.updateMenuById(sysMenu) ;
    }

    private void updateSysRoleMenuIsHalf(SysMenu sysMenu) {

        // 查询是否存在父节点
        SysMenu parentMenu = sysMenuMapper.selectById(sysMenu.getParentId());

        if (parentMenu != null) {

            // 将该id的菜单设置为半开
            sysRoleMenuMapper.updateSysRoleMenuIsHalf(parentMenu.getId());

            // 递归调用
            updateSysRoleMenuIsHalf(parentMenu);

        }
    }

}
